//
//  YZMessageListCell.m
//  CocoaAsyncSocketClient
//
//  Created by lixiaoyi on 2018/7/9.
//  Copyright © 2018年 lixiaoyi. All rights reserved.
//

#import "YZMessageListCell.h"
#import "Masonry.h"
#import "YZChatUtils.h"
#import "UIView+YZBadge.h"
#import "UIImageView+WebCache.h"

#define kDeleteButtonWidth      60.0f
#define kTagButtonWidth         100.0f
#define kCriticalTranslationX   30
#define kShouldSlideX           -2

@implementation YZMessageListCell
{
    UIButton *_deleteButton;
    UIButton *_tagButton;
    
    UIPanGestureRecognizer *_pan;
    UITapGestureRecognizer *_tap;
    
    BOOL _shouldSlide;
}


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupView];
        [self makeViewLayout];
    }
    
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.iconImageView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.messageLabel];
    [self.contentView insertSubview:_deleteButton belowSubview:self.contentView];
    [self.contentView insertSubview:_tagButton belowSubview:self.contentView];
}

- (void)makeViewLayout {
    // _deleteButton
    [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(self);
        make.width.mas_equalTo(kDeleteButtonWidth);
    }];
    
    // _tagButton
    [_tagButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self._deleteButton.mas_top);
        make.bottom.mas_equalTo(self._deleteButton.mas_bottom);
        make.right.mas_equalTo(self._deleteButton.mas_left);
        make.width.mas_equalTo(kTagButtonWidth);
    }];
    
    UIView *superView = self.contentView;
    CGFloat margin = 15;
    
    // _iconImageView
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(50);
        make.left.mas_equalTo(superView.mas_left).with.offset(margin);
        make.centerY.mas_equalTo(superView);
        make.right.mas_equalTo(self.nameLabel.mas_left).with.offset(-margin);
    }];
    
    // _nameLabel
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.iconImageView.mas_top);
        make.right.mas_equalTo(self.timeLabel.mas_left).with.offset(0);
        make.bottom.mas_equalTo(self.messageLabel.mas_top).with.offset(margin);
        make.left.mas_equalTo(self.iconImageView.mas_right).with.offset(margin);
        make.height.mas_equalTo(26);
    }];
    
    // _messageLabel
    [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).with.offset(margin);
        make.left.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(superView.mas_right).with.offset(margin);
        make.bottom.mas_equalTo(superView.mas_bottom).with.offset(margin);
    }];
    
    // _timeLabel
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(superView.mas_right).with.offset(-margin);
        make.width.mas_equalTo(120);
    }];
}

#pragma mark - lazy load
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
//        _iconImageView.layer.cornerRadius = 5;
//        _iconImageView.clipsToBounds = YES;
    }
    
    return _iconImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textColor = [UIColor blackColor];
    }
    
    return _nameLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor lightGrayColor];
        _timeLabel.textAlignment = NSTextAlignmentRight;
    }
    
    return _timeLabel;
}

- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [UILabel new];
        _messageLabel.font = [UIFont systemFontOfSize:14];
        _messageLabel.textColor = [UIColor lightGrayColor];
    }
    
    return _messageLabel;
}

- (UIButton *)_deleteButton {
    UIButton *deleBtn = [UIButton new];
    deleBtn.backgroundColor = [UIColor redColor];
    [deleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [deleBtn setTitle:@"删除" forState:UIControlStateNormal];
    [deleBtn addTarget:self action:@selector(deletBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    return deleBtn;
}

- (UIButton *)_tagButton {
    if (!_tagButton) {
        _tagButton = [UIButton new];
        _tagButton.backgroundColor = [UIColor lightGrayColor];
        [_tagButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_tagButton setTitle:@"标记未读" forState:UIControlStateNormal];
        [_tagButton addTarget:self action:@selector(tagButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _tagButton;
}


#pragma mark - event
- (void)deletBtnClick:(UIButton *)sender {

}

- (void)tagButtonClick:(UIButton *)sender {
    
}

- (void)setModel:(YZMessageListModel *)model {
    _model = model;
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.imageName] placeholderImage:nil];
    self.nameLabel.text = model.name;
    self.timeLabel.text = [YZChatUtils showChatTime:model.time];
    self.messageLabel.text = model.message;
    
    if (model.showRedHot) {
        [self.iconImageView setBadge:^(BadgeBuilder *builder) {
            builder.number = model.redHotNumber;
            builder.font = [UIFont systemFontOfSize:15];
            builder.size = CGSizeMake(6, 6);
        }];
    };
}

- (void)hideRedHot {
    [self.iconImageView setBadge:^(BadgeBuilder *builder) {
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
